#!/usr/bin/python
## Made a change
from pprint import pprint

words = open("/Users/jjc/Downloads/swampy.1.4/words.txt").read().splitlines()

## PALINDROMES ##
def palindrome(s):
	if len(s) <= 1:
		return True
	else:
		first, last, middle = s[0], s[-1], s[1:-1]
		if first != last:
			return False
		else:
			return palindrome(middle)
	
# print [x for x in words if palindrome(x)]

# s = "able was I ere I saw Elba"
# print palindrome(s.replace(' ','').lower())


## ANAGRAMS ##
def sortedTuple(s):
    s = list(s)
    s.sort()
    return tuple(s)

anagrams = {}

for word in words:
    w = sortedTuple(word)
    if w not in anagrams:
        anagrams[w] = [word]
    else:
        anagrams[w].append(word)

# Exercise 12.4

## (1)
analist = [x for x in anagrams.values() if len(x) > 1]

## (2)
asort = reversed(sorted(analist, key=lambda a: len(a)))

# for l in asort:
# 	print "%d anagrams of %d letters: %s" % (len(l),len(l[0]), l)

	
## (3) scrable bingos 7 letters + letter on the board
## answer: ['angriest', 'astringe', 'ganister', 'gantries', 'granites', 'ingrates', 'rangiest']
# sevenletters = [x for x in anagrams.keys() if len(x) == 7]
# 
# alphabet = list('abcdefghijklmnopqrstuvwxyz')
# 
# for letter in alphabet:
# 	for st in sevenletters:
# 		bingo = sortedTuple(''.join(st) + letter)
# 		if bingo in anagrams and len(anagrams[bingo]) == 7:
# 			print anagrams[bingo]
			
## (4) Two words form a "metathesis pair" if you can transform one into the other by swapping two letters; for example, "converse" and "conserve." Write a program that finds all of the metathesis pairs in the dictionary. Hint: don't test all pairs of words, and don't test all possible swaps.
## There are 3311 metathesis pairs in the words dictionary

# pairs = []
# for lst in analist:
# 	for x in lst:
# 		for y in lst:
# 			if len([z for z in zip(x,y) if z[0] != z[1]]) == 2:
# 				if (y,x) not in pairs:
# 					pairs.append((x,y))
# for x in sorted(pairs):
# 	print x
# 
## as a big ugly listcomp:
# for lst in analist:
# 	pairs = list(set([(x,y) for x in lst for y in lst if len([(a,b) for a,b in zip(x,y) if a != b]) == 2]))
# 	if pairs: print pairs



## Testing at the command line to find the above solution:
# zip('conserve','converse')
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 'v'), ('e', 'e'), ('r', 'r'), ('v', 's'), ('e', 'e')]
# 
# >>> for x in l:
# ...     for y in l:
# ...             print zip(x,y)
# ... 
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 's'), ('e', 'e'), ('r', 'r'), ('v', 'v'), ('e', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 'v'), ('e', 'e'), ('r', 'r'), ('v', 's'), ('e', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 's'), ('e', 'e'), ('r', 'e'), ('v', 'r'), ('e', 'v')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('v', 's'), ('e', 'e'), ('r', 'r'), ('s', 'v'), ('e', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('v', 'v'), ('e', 'e'), ('r', 'r'), ('s', 's'), ('e', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('v', 's'), ('e', 'e'), ('r', 'e'), ('s', 'r'), ('e', 'v')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 's'), ('e', 'e'), ('e', 'r'), ('r', 'v'), ('v', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 'v'), ('e', 'e'), ('e', 'r'), ('r', 's'), ('v', 'e')]
# [('c', 'c'), ('o', 'o'), ('n', 'n'), ('s', 's'), ('e', 'e'), ('e', 'e'), ('r', 'r'), ('v', 'v')]
# 
# 
# >>> for x in l:
# ...     for y in l:
# ...             print [z for z in zip(x,y) if z[0] != z[1]]
# ... 
# []
# [('s', 'v'), ('v', 's')]
# [('r', 'e'), ('v', 'r'), ('e', 'v')]
# [('v', 's'), ('s', 'v')]
# []
# [('v', 's'), ('r', 'e'), ('s', 'r'), ('e', 'v')]
# [('e', 'r'), ('r', 'v'), ('v', 'e')]
# [('s', 'v'), ('e', 'r'), ('r', 's'), ('v', 'e')]
# []
# 
# l = ['conserve','converse','conseerv']
# >>> for x in l:
# ...     for y in l:
# ...             if len([z for z in zip(x,y) if z[0] != z[1]]) == 2:
# ...                     print x
# ... 
# conserve
# converse
# >>> 
# 
# >>> [x for x in l for y in l if len([z for z in zip(x,y) if z[0] != z[1]]) == 2]
# ['conserve', 'converse']

# >>> [x for x in l for y in l if len([(a,b) for a,b in zip(x,y) if a != b]) == 2]
# ['conserve', 'converse']











